//
//  DDDExpressInterstitialVC.m
//  DDDAdDemo
//
//  Created by mu on 2020/7/27.
//  Copyright © 2020 summer. All rights reserved.
//

#import "DDDExpressInterstitialVC.h"
#import <DDDAdSdk/DDDAdSdk.h>
#import "MBProgressHUD.h"
@interface DDDExpressInterstitialVC ()<DDDNativeExpressInterstitialAdDelegate>

@property (nonatomic, strong) UIButton *upBtn;
@property (nonatomic, strong) UIButton *downBtn;

@property (nonatomic, strong) DDDNativeExpressInterstitialAd *interstitialAd;
@end

@implementation DDDExpressInterstitialVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat nextBtnW = 280.0;
    CGFloat nextBtnH = 45.0;
    CGFloat nextBtnX = ([UIScreen mainScreen].bounds.size.width - nextBtnW) * 0.5;
    CGFloat nextBtnY = 150.0;
    [self.view addSubview:self.upBtn];
    self.upBtn.frame = CGRectMake(nextBtnX, nextBtnY, nextBtnW, nextBtnH);
    
    nextBtnY = nextBtnY + nextBtnH + 20.0;
    [self.view addSubview:self.downBtn];
    self.downBtn.frame = CGRectMake(nextBtnX, nextBtnY, nextBtnW, nextBtnH);
    switch (self.type) {
        case 0:
            //插屏广告1：1
        {
            self.navigationItem.title = @"插屏广告1：1";
//            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_1_1 andRootViewController:self.navigationController andAdSize:CGSizeMake(300, 300)];
//            interstitialAd.delegate = self;
//            self.interstitialAd = interstitialAd;
            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_1_1  andAdSize:CGSizeMake(300, 300)];
            interstitialAd.delegate = self;
            self.interstitialAd = interstitialAd;
            
        }
            break;
        case 1:
            //插屏广告3：2
        {
            self.navigationItem.title = @"插屏广告3：2";
//            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_3_2 andRootViewController:self.navigationController andAdSize:CGSizeMake(300, 200)];
//            interstitialAd.delegate = self;
//            self.interstitialAd = interstitialAd;
            
            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_3_2  andAdSize:CGSizeMake(300, 200)];
            interstitialAd.delegate = self;
            self.interstitialAd = interstitialAd;
            
        }
            break;
        case 2:
            //插屏广告2：3
        {
            self.navigationItem.title = @"插屏广告2：3";
//            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_2_3 andRootViewController:self.navigationController andAdSize:CGSizeMake(300, 450)];
//            interstitialAd.delegate = self;
//            self.interstitialAd = interstitialAd;
            
            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_2_3  andAdSize:CGSizeMake(300, 450)];
            interstitialAd.delegate = self;
            self.interstitialAd = interstitialAd;
        }
            break;
        case 3:
            //横版插屏
        {
            self.navigationItem.title = @"横版插屏";
            
            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_Vertical  andAdSize:CGSizeZero];
            interstitialAd.delegate = self;
            self.interstitialAd = interstitialAd;
        }
            break;
        case 4:
            //横/竖版插屏
        {
            self.navigationItem.title = @"横/竖版插屏";
            
            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_VerticalHorizontal  andAdSize:CGSizeZero];
            interstitialAd.delegate = self;
            self.interstitialAd = interstitialAd;
        }
            break;
        case 5:
            //插屏广告2：3
        {
            self.navigationItem.title = @"全屏插屏-视频";
            
            DDDNativeExpressInterstitialAd *interstitialAd = [[DDDNativeExpressInterstitialAd alloc]initWithType:DDDInterstitialType_FullScreen  andAdSize:CGSizeZero];
            interstitialAd.delegate = self;
            self.interstitialAd = interstitialAd;
        }
            break;
        default:
            break;
    }
}

#pragma mark-- 点击事件
- (void)clickBtnSetAction:(UIButton *)sender {
    if (self.interstitialAd) {
        [self.interstitialAd loadAdData];
    }
}

- (void)clickBtnShowAction:(UIButton *)sender {
    if (self.interstitialAd && self.interstitialAd.isAdValid) {
        [self.interstitialAd showAdFromRootViewController:self.navigationController];
    } else {
        NSLog(@"视频播放还没有缓存成功");
    }
}
#pragma mark-- 懒加载
- (UIButton *)upBtn {
    if (!_upBtn) {
        _upBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_upBtn setTitle:@"获取素材" forState:UIControlStateNormal];
        [_upBtn addTarget:self action:@selector(clickBtnSetAction:) forControlEvents:UIControlEventTouchUpInside];
        [_upBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_upBtn setBackgroundColor:[UIColor redColor]];
        _upBtn.layer.masksToBounds = YES;
        _upBtn.layer.cornerRadius = 8.0;
    }
    return _upBtn;
}

- (UIButton *)downBtn {
    if (!_downBtn) {
        _downBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_downBtn setTitle:@"展示素材" forState:UIControlStateNormal];
        [_downBtn addTarget:self action:@selector(clickBtnShowAction:) forControlEvents:UIControlEventTouchUpInside];
        [_downBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_downBtn setBackgroundColor:[UIColor redColor]];
        _downBtn.layer.masksToBounds = YES;
        _downBtn.layer.cornerRadius = 8.0;
    }
    return _downBtn;
}

#pragma mark-- DDDNativeExpressInterstitialAdDelegate

/**
 广告材料成功加载时回调
 */
- (void)nativeExpresInterstitialAdDidLoad {
    NSLog(@"%s",__func__);
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.offset = CGPointMake(0, -100);
    hud.label.text = @"广告素材加载成功";
    [hud hideAnimated:YES afterDelay:2];
}

/**
 广告材料加载失败时回调
 @param errorCode : 错误状态
 */
- (void)nativeExpresInterstitialAdDidFailWithError:(NSInteger)errorCode {
    NSLog(@"%s",__func__);
    NSLog(@"summer--%ld",errorCode);
}

/**
 广告成功加载时回调
 */
- (void)nativeExpresInterstitialAdRenderSuccess {
    NSLog(@"%s",__func__);
}

/**
 广告无法呈现时回调
 @param errorCode : 错误状态
 */
- (void)nativeExpresInterstitialAdRenderFailError:(NSInteger)errorCode {
    NSLog(@"%s",__func__);
    NSLog(@"summer--%ld",errorCode);
}

/**
 广告即将显示时回调
 */
- (void)nativeExpresInterstitialAdWillVisible {
    NSLog(@"%s",__func__);
}

/**
 点击了广告的回调
 */
- (void)nativeExpresInterstitialAdDidClick {
    NSLog(@"%s",__func__);
}

/**
 广告将要关闭时的回调
 */
- (void)nativeExpresInterstitialAdWillClose {
    NSLog(@"%s",__func__);
}

/**
 广告关闭时的回调
 */
- (void)nativeExpresInterstitialAdDidClose {
    NSLog(@"%s",__func__);
}


/// 此方法在关闭另一个控制器时调用
- (void)nativeExpresInterstitialAdDidCloseOtherController {
    NSLog(@"%s",__func__);
}

@end
